<?php

	// +----------------------------------------------------------------------
	// | 分类控制器
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\controller\app\admin;

	use yicmf\builder\Edit;
	use yicmf\builder\Table;
	use think\facade\Url;
	use app\controller\admin\Common;
    use app\model\app\Modular as ModularModel;


	class Modular extends Common
	{
		protected function initialize()
		{
			parent::initialize();
		}

		public function _filter()
		{
		}

		public function index()
		{
			//读取规则列表
			$map = $this->_search($this->dbname);
			//            //显示页面
			$builder = new Table();
			$builder ->model(ModularModel::class)
                ->where($map)
                ->order('sort DESC')
                ->setSearchPostUrl('', '')
				->searchText('title', '搜索标题：', '标题')
				->buttonUpdate('update', '新增分类')
				->keyId()
                ->keyImageModel('cover_id', '在线素材')
                ->keyImageModel('image_id', '图片介绍')
                ->keyText('local_image', '打包素材')
                ->keyText('name','名称')
                ->keyBool('is_show','是否显示')
                ->keyBool('is_open','是否开放')
				->keyText('sort', '排序', 'sort')
				->actionUpdate();
			return $builder->fetch();
		}

		/**
		 * 编辑分类
		 * @access protected
		 * @author 微尘 <yicmf@qq.com>
		 */
		public function update()
		{
			if ($this->request->isPost()) { //提交表单
				 if ($this->request->param('id'))
                 {
                     $data = ModularModel::where('id',$this->request->param('id'))->find();
                     $info = $this->request->only(['name','local_image','content','image_id','url','cover_id','is_open','sort']);
                     $data->save($info);
                     $this->_returnSuccess( '更新成功');
                 }else{
                     ModularModel::create($this->request->param());

                     $this->_returnSuccess( '新增成功');
                 }
			} else {
				/* 获取分类信息 */
				$id = $this->request->get('id');
				$isEdit = $id ? true : false;
				if ($isEdit) {
					$data = ModularModel::where('id',$id)->find();
					if (!$data) {
						$this->_returnError( '分类不存在或者已经删除');
					}
				} else {
					$data['style'] = 3;
				}
			        $builder = new Edit();
				$builder->title('编辑分类栏目')
					->keyTextInline('name', '分类标识', '', '', 'required')
					->keyText('url', '跳转', '')
			        ->keyImageModel('cover_id','在线素材','优先使用在线素材')
                    ->keyTextInline('local_image', '打包素材', '')
			        ->keyImageModel('image_id','图片介绍','有图片优先图片，其次文本')
                    ->keyEditor('content','文本介绍')
                    ->keyBool('is_open','是否开放')
					->keySort()
					->data($data)
					->buttonSubmit('', '保存');
				//                    ->buttonClose();
				return $builder->fetch();
			}
		}
	}